package org.dormi.learn.product.port.persist.material;

import lombok.extern.slf4j.Slf4j;
import org.dormi.learn.product.domain.material.entity.Material;
import org.dormi.learn.product.domain.material.repo.MaterialRepository;
import org.dormi.learn.utils.TrackingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * add description here
 *
 * @author : dormi330
 * @version : 1.0
 */
@Component
@Slf4j
public class MaterialRepositoryImpl implements MaterialRepository {

    @Autowired private MaterialDAO productDAO;

    @Override
    public void saveMaterial(Material p) {
        if (!p._dirty()) {
            log.warn("skip save entity, type={} , but not dirty ", p.getClass().getName());
            return;
        }

        TrackingUtils.debugTrace();
        MaterialPO po = Converter.MAPPER.toPO(p);
        productDAO.save(po);

        //
        p.setId(po.getId());
        p._cleanDirty();
    }

    @Override
    public Material getById(Long id) {
        MaterialPO po = productDAO.getOne(id);
        if (null == po) return null;

        Material eo = Converter.MAPPER.toEO(po);
        eo._cleanDirty();

        return eo;
    }
}
